package com.sduept.nwld.dataserver.controller.message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.CellEditEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.component.message.model.PersonnelConfigure;
import com.sduept.nwld.mobile.manager.MobileUserInfoManager;
import com.sduept.nwld.mobile.model.MobileUserInfo;
import com.sduept.permission.entity.PersonnelEntityBean;

@Named
@ViewScoped
public class MobileUserInfoController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3555730574424234162L;

	@Autowired
	private MobileUserInfoManager mobileUserInfoManager;

	private List<PersonnelConfigure> mobileUser = new ArrayList<PersonnelConfigure>();
	private LazyDataModel<PersonnelConfigure> mobileUserInfo;
	private MobileUserInfo selected = null;// 前台单选的数据
	private boolean skip;// 前台校验
	private PersonnelConfigure created = new PersonnelConfigure();// 新建
	private PersonnelConfigure selectMu = null;// 所编辑的数据
	boolean deshowed = false;// 删除按钮是否显示
	private boolean recFlag; // 前台checkbox值
	private String mbUserName;// 用户姓名
	private List<MobileUserInfo> selfInfo = new ArrayList<MobileUserInfo>();// 查询个人账户信息
	private PersonnelEntityBean selectedUser;// 所选择的组织机构下的用户
	private String sysUserName;
	private String realName;
	private String account;

	@PostConstruct
	public void init() {
		initTable();
	}

	public void initTable() {

		mobileUser.clear();
		selectedUser = null;
		findAll();
		findSelfInfo();
		sysUserName = null;
		realName = null;
		account = null;
	}

	/**
	 * 刷新
	 */
	public void refresh() {

		initTable();
		mbUserName = null;
		selectedUser = null;
	}

	/**
	 * 查询全部
	 */
	public void findAll() {
		mobileUser.clear();
		mobileUserInfo = new LazyDataModel<PersonnelConfigure>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -4338193951577779599L;

			@Override
			public List<PersonnelConfigure> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				mobileUser = mobileUserInfoManager.findAllPerson(new int[] { first, first + pageSize });
				return mobileUser;
			}

			@Override
			public List<PersonnelConfigure> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
				mobileUser = mobileUserInfoManager.findAllPerson(new int[] { first, first + pageSize });
				return mobileUser;
			}

			@Override
			public PersonnelConfigure getRowData(String rowKey) {
				for (PersonnelConfigure m : mobileUser) {
					if (m.getId().equals(rowKey)) {
						return m;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(PersonnelConfigure object) {
				for (PersonnelConfigure m : mobileUser) {
					if (m.getId().equals(object.getId())) {
						return m;
					}
				}
				return null;
			}
		};
		mobileUserInfo.setRowCount(mobileUserInfoManager.countMobileUserInfo());
	}

	/**
	 * 检索
	 */
	public void search() {
		if ((null == mbUserName || "".equals(mbUserName))) {
			addErrorMessage("请输入搜索信息！");
		} else {
			mobileUser.clear();
			final String whereHql = " realName like ? ";
			final Object[] whereArgs = new Object[1];
			whereArgs[0] = "%" + mbUserName + "%";
			mobileUserInfo = new LazyDataModel<PersonnelConfigure>() {

				/**
				 * 
				 */
				private static final long serialVersionUID = -5083681736033783332L;

				@Override
				public List<PersonnelConfigure> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
					mobileUser = mobileUserInfoManager.findPersonByCondition(whereHql, whereArgs, new int[] { first, first + pageSize });
					return mobileUser;
				}

				@Override
				public List<PersonnelConfigure> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
					mobileUser = mobileUserInfoManager.findPersonByCondition(whereHql, whereArgs, new int[] { first, first + pageSize });
					return mobileUser;
				}

				@Override
				public PersonnelConfigure getRowData(String rowKey) {
					for (PersonnelConfigure m : mobileUser) {
						if (m.getId().equals(rowKey)) {
							return m;
						}
					}
					return null;
				}

				@Override
				public Object getRowKey(PersonnelConfigure object) {
					for (PersonnelConfigure m : mobileUser) {
						if (m.getId().equals(object.getId())) {
							return m;
						}
					}
					return null;
				}
			};
			mobileUserInfo.setRowCount(mobileUserInfoManager.countByCondition(whereHql, whereArgs));
		}
	}

	/**
	 * 新建
	 */
	public void createUser() {

		if (selectedUser == null) {
			addErrorMessage("新建失败，请先选择人员！");
		} else {
			String whereHql = " code = ?";
			Object[] whereArgs = new Object[1];
			whereArgs[0] = selectedUser.getCode();
			List<PersonnelConfigure> mobiList = mobileUserInfoManager.findPersonByCondition(whereHql, whereArgs, null);
			if (mobiList.size() > 0) {
				addErrorMessage("该人员已关联4A账号！");
			} else {
				created.setName(selectedUser.getName());
				created.setCode(selectedUser.getCode());
				created.setRealName(realName);
				created.setAccount(account);
				created = mobileUserInfoManager.createPerson(created);
				addSuccessMessage("新建成功!");
//				selectedUser = null;//清空当前所选择的人员
				initTable();
			}
		}
		sysUserName = null;
		realName = null;
		account = null;
	}

	/**
	 * 前台wizard事件
	 * @param event
	 * @return
	 */
	public String onFlowProcess(FlowEvent event) {
		if (skip) {
			skip = false; // reset in case user goes back
			return "confirm";
		} else {
			return event.getNewStep();
		}
	}

	/**
	 * 组织机构下人员选择
	 */
	public void chooseUser() {

		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", false);
		options.put("modal", true);
		options.put("width", 400);
		options.put("height", 600);
		openDialog("/ui/common/org_selector_user_single", null, options);
	}

	/**
	 * 组织机构下人员选择
	 */
	public void onUsersChosen(SelectEvent event) {

		// 返回的是List<PersonnelEntityBean>
		selectedUser = (PersonnelEntityBean) event.getObject();
		sysUserName = selectedUser.getName();
		realName = selectedUser.getName();
	}

	/**
	 * 编辑
	 * @param event
	 */
	public void editUser(CellEditEvent event) {
		selectMu = null;
		for (PersonnelConfigure mu : mobileUser) {
			if (mu.getId().equals(event.getRowKey())) {
				selectMu = mu;
				break;
			}
		}
		if (selectMu != null) {
			mobileUserInfoManager.updatePerson(selectMu);
		}
		addSuccessMessage("修改成功!");
	}

	public void save() {
		mobileUserInfoManager.updatePerson(selectMu);
		addSuccessMessage("修改成功!");
	}

	public void editUserFlag(MobileUserInfo user) {
		/*
		 * if (selectMu == null) { addErrorMessage("请选择要修改的用户信息"); }else{
		 * selectMu.setReceiveFlag(recFlag);
		 */
//			mobileUserInfoManager.updateUser(user);
		addSuccessMessage("修改成功!");
		// }
	}

	/**
	 * 删除
	 */
	public void deleteUser() {

		if (selectMu != null) {

			mobileUserInfoManager.deletePerson(selectMu);// 数据库删
			mobileUser.remove(selectMu);// 前台删
			selectMu = null;
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
		addSuccessMessage("删除成功!");
		deshowed = false;
		initTable();
	}

	/**
	 * 查询个人账户信息
	 * @return
	 */
	public List<MobileUserInfo> findSelfInfo() {
		PersonnelEntityBean currentUser = getCurrentUser();
		selfInfo = mobileUserInfoManager.findSelfInfo(currentUser);
		return selfInfo;
	}

	/**
	 * 编辑
	 * @param event
	 */
	public void editSelfInfo(CellEditEvent event) {
//		selectMu = null;
//		for (MobileUserInfo mu : selfInfo) {
//			if (mu.getId().equals(event.getRowKey())) {
//				
//				selectMu = mu;
//				break;
//			}
//		}
//		if (selectMu != null) {
//			mobileUserInfoManager.updateUser(selectMu);
//		}
		addSuccessMessage("修改成功!");
	}

	public void onRowSelect(SelectEvent event) {
		selectMu = (PersonnelConfigure) event.getObject();
		deshowed = true;
	}

	public MobileUserInfoManager getMobileUserInfoManager() {
		return mobileUserInfoManager;
	}

	public void setMobileUserInfoManager(MobileUserInfoManager mobileUserInfoManager) {
		this.mobileUserInfoManager = mobileUserInfoManager;
	}

	public List<PersonnelConfigure> getMobileUser() {
		return mobileUser;
	}

	public void setMobileUser(List<PersonnelConfigure> mobileUser) {
		this.mobileUser = mobileUser;
	}

	public LazyDataModel<PersonnelConfigure> getMobileUserInfo() {
		return mobileUserInfo;
	}

	public void setMobileUserInfo(LazyDataModel<PersonnelConfigure> mobileUserInfo) {
		this.mobileUserInfo = mobileUserInfo;
	}

	public MobileUserInfo getSelected() {
		return selected;
	}

	public void setSelected(MobileUserInfo selected) {
		this.selected = selected;
	}

	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public PersonnelConfigure getCreated() {
		return created;
	}

	public void setCreated(PersonnelConfigure created) {
		this.created = created;
	}

	public boolean isDeshowed() {
		return deshowed;
	}

	public void setDeshowed(boolean deshowed) {
		this.deshowed = deshowed;
	}

	public boolean isRecFlag() {
		return recFlag;
	}

	public void setRecFlag(boolean recFlag) {
		this.recFlag = recFlag;
	}

	public PersonnelConfigure getSelectMu() {
		return selectMu;
	}

	public void setSelectMu(PersonnelConfigure selectMu) {
		this.selectMu = selectMu;
	}

	public String getMbUserName() {
		return mbUserName;
	}

	public void setMbUserName(String mbUserName) {
		this.mbUserName = mbUserName;
	}

	public List<MobileUserInfo> getSelfInfo() {
		return selfInfo;
	}

	public void setSelfInfo(List<MobileUserInfo> selfInfo) {
		this.selfInfo = selfInfo;
	}

	public PersonnelEntityBean getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(PersonnelEntityBean selectedUser) {
		this.selectedUser = selectedUser;
	}

	public String getSysUserName() {
		return sysUserName;
	}

	public void setSysUserName(String sysUserName) {
		this.sysUserName = sysUserName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}
}
